package com.oceansoft.mobile.biz.admin.controller;

import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.oceansoft.mobile.biz.admin.entity.Profile;
import com.oceansoft.mobile.biz.admin.entity.SysUser;
import com.oceansoft.mobile.biz.admin.service.ISysUserService;
import com.oceansoft.mobile.common.Constant;
import com.oceansoft.mobile.common.base.entity.Result;
import com.oceansoft.mobile.weixin.util.MD5Util;

@Controller
@RequestMapping("admin")
public class SysAdminController {

	@Resource
	private ISysUserService sysUserService;
	
    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true));
    }

    @RequestMapping("index")
    public String gotoIndex(HttpSession session, Model model) {
        return "admin/admin-index";
    }
    
    @RequestMapping("merIndex")
    public String gotoMerIndex(HttpSession session, Model model) {
        return "admin/merAdmin-index";
    }

    
    @RequestMapping(value = "resetpwd", method = GET)
    public String gotoModifyPasswd() {
        return "admin/reset-pwd";
    }

    @ResponseBody
    @RequestMapping(value = "resetpwd", method = POST)
    public Result resetPassword(Profile profile, HttpSession session) {
        if (null == profile)
            return new Result(Result.FAILURE, "提交参数错误");
        if (!StringUtils.hasText(profile.getOrigiPwd())) {
            return new Result(Result.FAILURE, "请输入原始密码");
        }
        if (!StringUtils.hasText(profile.getNewPwd())) {
            return new Result(Result.FAILURE, "请输入新密码");
        }
        if (!StringUtils.hasText(profile.getConfirmPwd())) {
            return new Result(Result.FAILURE, "请输入确认密码");
        }
        if (!(profile.getNewPwd().equals(profile.getConfirmPwd()))) {
            return new Result(Result.FAILURE, "两次输入的输入不一致");
        }
        SysUser user = ((SysUser) session.getAttribute(Constant.GLOBAL_ADMIN_SESSION));
        if(null == user)
        	user = ((SysUser) session.getAttribute(Constant.GLOBAL_SESSION));
        return sysUserService.updatePassword(user.getId(), MD5Util.getMD5String(profile.getOrigiPwd()), MD5Util.getMD5String(profile.getNewPwd()));
    }
    
    public static void main(String[] args) {
		System.out.println(MD5Util.getMD5String("Szga@2015!"));
	}
    
    /**
     * 进入后台商家管理界面
     *
     * @return String
     */
    @RequestMapping(value = "merchant/merchantList", method = GET)
    public String gotoMerchantList() {
        return "admin/merchant-list";
    }

   
}
